package bg.tu.varna.pms.client.gui;

import java.awt.event.ActionListener;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListModel;

import bg.tu.varna.pms.client.controller.MypeActionCommand;


public class UserGroupPanel extends JPanel {

	private static final long serialVersionUID = -2734725665553936712L;

	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	
	private AddUserDialog addUserDialog = null;
	private RemoveUserDialog removeUserDialog = null;
	private AddGroupDialog addGroupDialog = null;
	private RemoveGroupDialog removeGroupDialog = null;
	
	public UserGroupPanel() {
		initComponents();
	}
	
	public UserGroupPanel(final MouseListener controller) {
		this();
		
		addController(controller);
	}
	
	public void addController(final MouseListener controller) {
		jLabel1.setName(MypeActionCommand.SHOW_ADD_USER_PANEL);
		jLabel1.addMouseListener(controller);
		
		jLabel2.setName(MypeActionCommand.SHOW_REMOVE_USER_PANEL);
		jLabel2.addMouseListener(controller);
		
		jLabel3.setName(MypeActionCommand.SHOW_ADD_GROUP_PANEL);
		jLabel3.addMouseListener(controller);
		
		jLabel4.setName(MypeActionCommand.SHOW_REMOVE_GROUP_PANEL);
		jLabel4.addMouseListener(controller);
	}
	
	private void initComponents() {
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		
		jLabel1.setIcon(MediaManager.getInstance().getIconAddUser());

		jLabel2.setIcon(MediaManager.getInstance().getIconDeleteUser());

		jLabel3.setIcon(MediaManager.getInstance().getIconAddGroup());

		jLabel4.setIcon(MediaManager.getInstance().getIconDeleteGroup());
		
		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				this);
		this.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addComponent(jLabel1)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jLabel2)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jLabel3)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jLabel4)
										.addGap(0, 0, Short.MAX_VALUE)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(jLabel2)
														.addComponent(jLabel3)
														.addComponent(jLabel4))
										.addContainerGap(17, Short.MAX_VALUE)));
	}

	public void showAddUserDialog(final ActionListener controller) {
		if (addUserDialog == null) {
			addUserDialog = new AddUserDialog((JFrame)getTopLevelAncestor(), true, controller);
		}
		
		addUserDialog.setVisible(true);
	}

	public void showRemoveUserDialog(final ActionListener controller) {
		if (removeUserDialog == null) {
			removeUserDialog = new RemoveUserDialog((JFrame)getTopLevelAncestor(), true, controller);
		}
		
		removeUserDialog.setVisible(true);
	}

	public void showAddGroupDialog(final ActionListener controller) {
		if (addGroupDialog == null) {
			addGroupDialog = new AddGroupDialog((JFrame)getTopLevelAncestor(), true, controller);
		}
		
		addGroupDialog.setVisible(true);
	}

	public void showRemoveGroupDialog(final ActionListener controller) {
		if (removeGroupDialog == null) {
			removeGroupDialog = new RemoveGroupDialog((JFrame)getTopLevelAncestor(), true, controller);
		}
		
		removeGroupDialog.setVisible(true);
	}

	public void setUsersError(String message) {
		JOptionPane.showMessageDialog(this, "The following error occured: " + message, "Error!" ,JOptionPane.ERROR_MESSAGE);
	}

	public void setUserSearchListModel(ListModel<String> model) {
		addUserDialog.setUserListModel(model);
	}

	public String getUserNameToAdd() {
		return addUserDialog.getUserNameToAdd();
	}

	public String getUserNameToRemove() {
		return removeUserDialog.getUserNameToRemove();
	}

	public String getUserNameToFind() {
		return addUserDialog.getUserNameToFind();
	}
}
